#
# tdumper.py
#
#
# PyEMOF: An EMOF support for Python
#
# Copyright (C) 2004, 2007 Raphael Marvie 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
# http://www.fsf.org/licensing/licenses/gpl.html
#
# Author contact: raphael.marvie@lifl.fr
#
# Modified and adapted to conform with ECORE 2.0 by brenomachado@gmail.com



'''
Templates for the generation of Loader and Dumper utilities for
repository generated used PyEMOF.
'''

__author__  = Raphael Marvie <raphael.marvie@lifl.fr> / 'Breno Batista Machado <brenomachado@gmail.com>'
__date__    = '2008-03-16'
__version__ = '0.7.0'

class tstr:
    pass

class tmpl:
    
    file_header = """# -*- coding: utf-8 -*-
#
# dumper.py
#

from xml.dom import minidom

%(modules_imports)s

class DumpElement (object):
    def __init__ (self, doc):
        super(DumpElement, self).__init__()
        self._doc = doc
    def process(self, obj, elt):
        elt.setAttributeNS('xmi', 'xmi:id', obj._xmi_id)
    def __call__(self, obj, nodename):
        elt = self._doc.createElement(nodename)
        self.process(obj, elt)
        return elt
"""

    import_template = """import %(module)s"""

    dump_dict = """
dumpers = {
%(values)s    
}
"""

    dumper_class = """
class Dumper (object):
    '''%(name)s model dumper to an XMI file.'''

    def __init__(self):
        super(Dumper, self).__init__()
        self._doc = minidom.Document()
        root = self._doc.createElement('xmi:XMI')
        root.setAttribute('xmi:version', '2.1')
        root.setAttribute('xmlns:xmi', 'http://www.omg.org/XMI')
        root.setAttribute('xmlns:xsi',
                          'http://www.w3.org/2001/XMLSchema-instance' )
        root.setAttribute('xmlns:%(name)s',
                          'http://nowhere/%(name)s.xmi')
        self._doc.appendChild(root)   

    def dump(self, rep):
"""

    dumper_main_body = """        for x in rep.%(module)s%(name)s:
            if x.isRoot:
                elt = %(name)s(self._doc)(x, '%(prefix)s:%(name)s')
                self._doc.documentElement.appendChild(elt)
"""

    dumper_main_end = """        return self._doc.toprettyxml()
"""

    dump_primitive = """

class %(name)s (DumpElement):

    def __init__(self, doc):
        super(%(name)s, self).__init__(doc)

    def process(self, obj, elt):
        super(Element, self).process(obj, elt)
        if elt.nodeName == '%(prefix)s:%(name)s':
            try:
                elt.removeAttributeNS('xmi', 'type')
            except:
                pass
        else:
            elt.setAttributeNS('xmi', 'xmi:type', '%(prefix)s:%(name)s')

"""

    dump_element_begin = """

class %(name)s (%(super)s):

    def __init__(self, doc):
        super(%(name)s, self).__init__(doc)

    def process(self, obj, elt):
        super(%(name)s, self).process(obj, elt)
        if elt.nodeName == '%(prefix)s:%(name)s':
            try: 
                elt.removeAttributeNS('xmi', 'type')
            except:
                pass
        else:
            elt.setAttributeNS('xmi', 'xmi:type', '%(prefix)s:%(name)s')
"""

    dump_mult_composite = """        for o in obj.%(iname)s:
            clss = dumpers[type(o)]
            child = clss(self._doc)(o, '%(xname)s')
            elt.appendChild(child)
"""
#falta
    dump_primitive_composite = """        for o in obj.%(iname)s:
            child = %(xtype)s(self._doc)(o, '%(xname)s')
            elt.appendChild(child)
"""
#falta
    dump_single_composite = """        if obj.%(iname)s:
            clss = dumpers[type(obj.%(iname)s)]
            child = clss(self._doc)(obj.%(iname)s, '%(xname)s')
            elt.appendChild(child)
"""

    dump_mult_attribute = """        for o in obj.%(iname)s:
            child = self._doc.createElement('%(xname)s')
            child.setAttributeNS('xmi', 'xmi:type', ('%(prefix)s:' + o.__class__.__name__))
            child.setAttributeNS(None, 'idref', o._xmi_id)
            elt.appendChild(child)            

"""

    dump_attribute = """        if obj.%(iname)s:
            elt.setAttributeNS(None, '%(xname)s', str(obj.%(iname)s))
"""

    dump_attr_typed = """        if obj.%(iname)s:
            elt.setAttributeNS(None, '%(xname)s', obj.%(iname)s._xmi_id)
"""

    dump_attr_enum = dump_attribute
    
    file_footer = """
# eof
"""
